package com.cart;

import com.cart.controller.AdminController;
import com.cart.controller.CustomerActionsController;
import com.cart.models.Admin;
import com.cart.models.Customer;
import com.cart.models.User;
import com.cart.services.*;
import com.cart.utils.AdminFileUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Scanner;

public class App {

    private static UserRepository userRepository;

    static {
        try {
            userRepository = new UserRepositoryImpl();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    private static ProductRepository productRepository;

    static {
        try {
            productRepository = new ProductRepositoryImpl();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    private static OrderHistoryRepository orderHistoryRepository;

    static {
        try {
            orderHistoryRepository = new OrderHistoryRepositoryImpl();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws InvocationTargetException {
        // 当程序退出时，保存数据到文件
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            userRepository.saveToFile();
            productRepository.saveToFile();
            orderHistoryRepository.saveToFile();
            System.out.println("数据已保存到文件！");
        }));
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎使用购物系统");
            System.out.println("1. 管理员登录");
            System.out.println("2. 客户登录");
            System.out.println("3. 注册客户");
            System.out.println("4. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine();  // 清理换行符

            switch (choice) {
                case 1:
                    adminLogin(scanner);
                    break;
                case 2:
                    customerLogin(scanner);
                    break;
                case 3:
                    registerCustomer(scanner);
                    break;
                case 4:
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("无效选择，请重试。");
            }
        }
    }

    private static void adminLogin(Scanner scanner) {
        // 从文件读取管理员信息
        Admin admin = AdminFileUtil.readAdminFromFile();

        System.out.print("请输入管理员用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入管理员密码: ");
        String password = scanner.nextLine();

        // 验证用户名和密码
        if (admin.getUsername().equals(username) && admin.getPassword().equals(User.md5Hash(password))) {
            System.out.println("管理员登录成功！");
            AdminController.adminActions(admin, userRepository, productRepository, orderHistoryRepository, scanner);
        } else {
            System.out.println("管理员用户名或密码错误！");
        }
    }


    // 客户登录
    private static void customerLogin(Scanner scanner) {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        User user = userRepository.findByUsername(username);
        if (user != null && user.getPassword().equals(Admin.md5Hash(password))) {
            if ("1".equals(user.getType())) { // 普通客户
                Customer customer = new Customer(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(), user.getUserLevel(), user.getRegistrationDate());
                System.out.println("客户登录成功！");
                Boolean res = CustomerActionsController.customerActions(customer, userRepository, productRepository, orderHistoryRepository, scanner);
            } else {
                System.out.println("用户类型错误！");
            }
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    // 注册客户
    private static void registerCustomer(Scanner scanner) throws InvocationTargetException {
        System.out.print("请输入id:");
        int id = Integer.parseInt(scanner.nextLine());
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();
        System.out.print("请输入邮箱: ");
        String email = scanner.nextLine();
        System.out.print("请输入电话号码: ");
        String phone = scanner.nextLine();

        User newUser = new User(id, username, password, email, phone, "铜牌客户", new Date().toString(), "1");
        userRepository.save(newUser);
        System.out.println("注册成功！");
    }
}
